home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / MENU_BUT.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  7.5 KB  |  272 lines

  1. package sub_arctic.lib;
  2. import sub_arctic.input.*;
  3. import sub_arctic.output.style;
  4. import sub_arctic.output.loaded_image;
  5. import sub_arctic.output.style_manager;
  6.  
  7. import java.awt.Point;
  8. import java.awt.Font;
  9. /*
  10.  * This class implements a button which can pop up a menu when
  11.  * pressed on. 
  12.  * 
  13.  * @author Ian Smith
  14.  */
  15. public class menu_button 
  16. extends multi_button implements pressable, menu_notifier {
  17.  
  18.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  19.  
  20.   /**
  21.    * The x spacing for this object.
  22.    */
  23.   protected int _x_spacing=10;
  24.  
  25.   /**
  26.    * Get the x spacing. 
  27.    * @return int the amount of x spacing
  28.    */
  29.   public int x_spacing() { return _x_spacing;}
  30.  
  31.   /**
  32.    * Set the x spacing.
  33.    * @param int x the new value for the x spacing
  34.    */
  35.   public void set_x_spacing(int x) { 
  36.     _x_spacing=x;
  37.     style_changed();
  38.   }
  39.  
  40.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  41.  
  42.   /**
  43.    * The y spacing for this object.
  44.    */
  45.   protected int _y_spacing=5;
  46.  
  47.   /**
  48.    * Get the y spacing. 
  49.    * @return int the amount of y spacing
  50.    */
  51.   public int y_spacing() { return _y_spacing;}
  52.  
  53.   /**
  54.    * Set the y spacing.
  55.    * @param int y the new value for the y spacing
  56.    */
  57.   public void set_y_spacing(int y) { 
  58.     _y_spacing=y;
  59.     style_changed();
  60.   }
  61.  
  62.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  63.  
  64.   /**
  65.    * This holds a pointer to our menu that we will pop up. 
  66.    */
  67.   menu _menu;
  68.  
  69.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  70.  
  71.   /**
  72.    * This function is called in response to a press method.
  73.    * @param event    e         the event to dispatch (the mouse down).
  74.    * @param Object   user_info (currently ignored) the object passed to the 
  75.    *                           pick_collector at pick-time.
  76.    * @return boolean true      if this event was dispatched.
  77.    */
  78.   public boolean press(event e, Object user_info /* ignored */) {
  79.     style cs=style_manager.current_style();
  80.  
  81.     /* make us change state */
  82.     set_cur_state(1);
  83.     
  84.     /* get a top level and get the top left point of our rectangle
  85.        in its coord sys */
  86.     top_level tl=get_top_level();
  87.     Point p=new Point(0,0);
  88.     p=local_to_global(p);
  89.     
  90.     /* put the new menu in the toplevel at the right spot */
  91.     /* test for pop down or pop right */
  92.     if (cs.menu_pop_right()) {
  93.       _menu.set_x(p.x+w());
  94.       _menu.set_y(p.y);
  95.     } else {
  96.       _menu.set_x(p.x);
  97.       _menu.set_y(p.y+h());
  98.     }
  99.  
  100.     tl.add_child(_menu);
  101.     menu.agent().set_focus_to(_menu,e,null,this); /* no user info for now */
  102.     return true;
  103.   }
  104.  
  105.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  106.  
  107.   /* 
  108.    * This never gets called because we make the other object the 
  109.    * menu drag focus. 
  110.    *
  111.    * @param event  e         the event to dispatch.
  112.    * @param Object user_info (currently ignored) the object passed to the 
  113.    *                         pick_collector at pick-time.
  114.    * @return boolean true if this event was dispatched.
  115.    */
  116.   public boolean release(event e, Object user_info /*ignored */) {
  117.     return false;
  118.   }
  119.  
  120.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  121.  
  122.   /**
  123.    * The text string on this object.
  124.    */
  125.   protected String _text;
  126.  
  127.   /**
  128.    * Retrieve the text value on this object.
  129.    * @return String the text on this menu button
  130.    */
  131.   public String text() { return _text;}
  132.  
  133.   /**
  134.    * Set the text of this object.
  135.    * @param String t the new text to display on this menu button
  136.    */
  137.   public void set_text(String t) { 
  138.     _text=t;
  139.     style_changed();
  140.   }
  141.  
  142.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  143.  
  144.   /**
  145.    * The font for this object.
  146.    */
  147.   protected Font _font=null;
  148.  
  149.   /**
  150.    * Get the font associated with this object.  This will return the
  151.    * system's default font if you haven't set the font.
  152.    * @return Font the font used to display this menu button's text
  153.    */
  154.   Font font() { 
  155.     if (_font==null) {
  156.       return style_manager.default_font();
  157.     } else {
  158.       return _font;
  159.     }
  160.   }
  161.  
  162.   /**
  163.    * Set the font associated with this object. If you set the font
  164.    * to null, you'll get the system default font. 
  165.    * @param Font f the new font
  166.    */
  167.   public void set_font(Font f) {
  168.     _font=f;
  169.     style_changed();
  170.   }
  171.  
  172.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  173.  
  174.   /**
  175.    * Redraw the images for this menu button. 
  176.    */
  177.   protected void style_changed() {
  178.     loaded_image[] img;
  179.     img=style_manager.current_style().button_make_images(text(),
  180.                              font(),
  181.                              x_spacing(),
  182.                              y_spacing(),
  183.                              true);
  184.     set_looks(img,null);
  185.   }
  186.  
  187.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  188.  
  189.   /**
  190.    * Construct a menu button given a string. We assume you will use 
  191.    * constraints (or direct setting) to position this object.
  192.    * @param String s the string to display on the menu button.
  193.    * @param menu   m the menu to pop up when the button is pressed. 
  194.    */
  195.   public menu_button(String s, menu m) {
  196.     super(0,0,null,null,null);
  197.     _menu=m;
  198.     set_text(s);
  199.   }
  200.  
  201.    //had:
  202.    //* @exception general PROPAGATED
  203.  
  204.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  205.  
  206.   /**
  207.    * Construct a menu button given a string and a given font. 
  208.    * We assume you will use constraints to position this object
  209.    * or set the position directly.
  210.    * 
  211.    * @param String s the string to display on the menu button.
  212.    * @param Font   f font we draw labels in.
  213.    * @param menu   m the menu to pop up when the button is pressed.
  214.    */
  215.   public menu_button(String s,Font f, menu m){
  216.     super(0,0,null,null,null);
  217.     _font=f;
  218.     _menu=m;
  219.     set_text(s);
  220.   }
  221.  
  222.    //had:
  223.    //* @exception general PROPAGATED
  224.  
  225.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  226.  
  227.   /**
  228.    * This method gets called by the menu_agent to inform us that
  229.    * the interaction is completed.
  230.    */
  231.   public void menu_done() {
  232.     /* set our look to the up state ... just to make sure */
  233.     set_cur_state(0);
  234.   }
  235.  
  236.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  237.  
  238.   /**
  239.    * This function is called to alert the notifier that the interaction
  240.    * is now over their area. The notifier should return true if it
  241.    * modified the set of objects in the menu focus in response to
  242.    * this call. Menu_buttons don't use this call and always return false.
  243.    *
  244.    * @param int   x   the x coordinate (in the notifiers coordinate system) of 
  245.    *                  the cursor.
  246.    * @param int   y   the y coordinate (in the notifiers coordinate system) of 
  247.    *                  the cursor.
  248.    * @param event evt the event we are testing.
  249.    * @return boolean true if the notifier modified the focus set of the menu 
  250.    *                 agent in response to this.
  251.    */
  252.   public boolean menu_modify(int x,int y, event evt) { return false;}
  253.  
  254.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  255. }
  256. /*=========================== COPYRIGHT NOTICE ===========================
  257.  
  258. This file is part of the subArctic user interface toolkit.
  259.  
  260. Copyright (c) 1996 Scott Hudson and Ian Smith
  261. All rights reserved.
  262.  
  263. The subArctic system is freely available for most uses under the terms
  264. and conditions described in 
  265.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  266. and appearing in full in the lib/interactor.java source file.
  267.  
  268. The current release and additional information about this software can be 
  269. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  270.  
  271. ========================================================================*/
  272.